const Staff = require('../../model/Staff')
const Contempt = require('../../model/Contempt')
const Post = require('../../model/Post')
const { Op } = require('sequelize')

// 添加一个职位数据
exports.addStaff = async function (data) {
    const res = await Staff.create(data)
    console.log(res)
}

// 添加一组职位数据
exports.addStaffs = async function (data) {
    const res = await Staff.bulkCreate(data)
    console.log(res)
}
// 更新一个职位
exports.updateStaffs = async function (id, data) {
    const res = await Staff.update(data, {
        where: {
            id
        }
    })
    console.log(res)
}
// 删除一个职位
exports.deleteStaff = async function (id) {
    const res = await Staff.destroy({
        where: id
    })
    console.log(res)
}
// 查询职位
exports.selectStaff = async function (condition = {}) {
    const where = { ...condition }
    if (condition.name) {
        where.name = {
            [Op.substring]: condition.name,
        }
    }
    const res = await Staff.findAll({
        attributes: ['id', 'name'],
        include: [{ model: Contempt }, { model: Post }],
        where
    })

    return JSON.parse(JSON.stringify(res))
}